package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.constant.DeviceOptions;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_AlarmDataServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.collect.util.DateUtil;
import com.hzhr.iot.collect.util.StrUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataPostModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 集中器 集中器异常事件上报
 * @author 48310
 *
 */
@Component
public class MessageProcessor88 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_AlarmDataServiceHelper device_alarmDataServiceHelper;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	DeviceOptions deviceOptions;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		//查询物联网设备
		IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByIotPlatformDeviceId(deviceId);
		Integer iotDeviceId = iotDeviceModel.getId();


		//如果报警数据不存在，则写入报警数据
		String currentDate = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE));
		String currentTime = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME));
		String alarmType = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.ALARM_TYPE));
		Date currentTm = DateUtil.strToDate(currentDate + currentTime, "yyyyMMddHHmmss");

		int existCount = device_alarmDataServiceHelper.getCountByIotDeviceIdAndCurrentTm(iotDeviceId, currentTm);
		if (existCount == 0) {
			AlarmDataPostModel alarmDataPostModel = new AlarmDataPostModel();
			alarmDataPostModel.setIotDeviceId(iotDeviceId);
			alarmDataPostModel.setCurrentTm(currentTm.getTime());
			if ("1001".equals(alarmType)) {//强行关阀
				alarmDataPostModel.setAlarmType("1031");
			} else if ("1002".equals(alarmType)) {//燃气泄露
				alarmDataPostModel.setAlarmType("1032");
			} else if ("1005".equals(alarmType)) {//存储器坏
				alarmDataPostModel.setAlarmType("1034");
			} else if ("1006".equals(alarmType)) {//强磁干扰
				alarmDataPostModel.setAlarmType("1035");
			} else if ("1007".equals(alarmType)) {//死表故障
				alarmDataPostModel.setAlarmType("1036");
			}  else if ("1009".equals(alarmType)) {//电池故障
				alarmDataPostModel.setAlarmType("1038");
			}
			device_alarmDataServiceHelper.post(alarmDataPostModel);
		}

		//组装响应
		List<Map<MessageFieldKeyEnum, Object>> responseMsgList = new ArrayList<Map<MessageFieldKeyEnum, Object>>();
		String ccrNo = StrUtil.removeZeroForLeft(parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString());
		Map<MessageFieldKeyEnum, Object> respMsg = this.messageUtil.generateCcrResponse(protocolId, parsedMessageObj, "0000", ccrNo);
		responseMsgList.add(respMsg);
		return responseMsgList;
	}
}
